/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>Project: javaweb - MyServletRequestListener
 * <p>Powered by webrx On 2022-03-10 16:45:19
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@WebListener
public class MyServletRequestListener  implements ServletRequestListener, ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {

    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {

    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {

    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        //System.out.println("requestDestroyed（销毁）");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest req = (HttpServletRequest) sre.getServletRequest();
        HttpSession session = req.getSession();
        if(session.getAttribute("ip") == null){
            session.setAttribute("ip",req.getRemoteAddr()+",时间:"+String.format("%tF %<tT",System.currentTimeMillis()));
        }
        //System.out.println("客户的IP"+sre.getServletRequest().getRemoteAddr());
        //System.out.println("requestInitialize（初始化）)");
    }
}
